//
//  SomeTestVC.swift
//  elarc01
//
//  Created by elarc on 2016/11/7.
//  Copyright © 2016年 elarc. All rights reserved.
//

class ContainerViewController: UIViewController,UIScrollViewDelegate {
    
    var menuContainerView:UIView!
    var scrollView: UIScrollView!
    var detailViewController:DetailViewController?
    
    var menuItem: NSDictionary? {
        didSet {
            hideOrShowMenu(false, animated: true)
            if let detailViewController = detailViewController {
                detailViewController.menuItem = menuItem
            }
        }
    }
    
    var showingMenu = false
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        menuContainerView.layer.anchorPoint = CGPointMake(1.0, 0.5)
        hideOrShowMenu(showingMenu, animated: false)
    }
    
    func hideOrShowMenu(show:Bool,animated:Bool) -> Void {
        let xOffset = CGRectGetWidth(menuContainerView.bounds)
        scrollView.setContentOffset(show ? CGPointZero: CGPointMake(xOffset, 0), animated: animated)
        showingMenu = show
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let mutiplier = 1.0 / CGRectGetWidth(menuContainerView.bounds)
        let offset = scrollView.contentOffset.x * mutiplier
        let fraction = 1.0 - offset
        
        menuContainerView.layer.transform = transformForFraction(fraction)
        menuContainerView.alpha = fraction
        
        if let detailViewController = detailViewController{
            if let rotatingView = detailViewController.hamburgerView{
                rotatingView.rotate(fraction)
            }
        }
        
        scrollView.pagingEnabled = scrollView.contentOffset.x < (scrollView.contentSize.width - CGRectGetWidth(scrollView.frame))
        
        let menuOffset = CGRectGetWidth(menuContainerView.bounds)
        showingMenu = !CGPointEqualToPoint(CGPoint(x: menuOffset, y: 0), scrollView.contentOffset)

    }
    
//    转一个
    func transformForFraction(fraction:CGFloat) -> CATransform3D {
        var identity = CATransform3DIdentity
        identity.m34 = -1/1000
        let angle = Double(1.0 - fraction) * -M_PI_2
        let xOffset = CGRectGetWidth(menuContainerView.bounds) * 0.5
        let rotateTransform = CATransform3DRotate(identity, CGFloat(angle), 0.0, 1.0, 0.0)
        let translateTransform = CATransform3DMakeTranslation(xOffset, 0.0, 0.0)
        return CATransform3DConcat(rotateTransform, translateTransform)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.randomColor()
        configSubViews()
        
        let btn = DragButton()
        btn.frame = CGRectMake(100, 100, 100, 40)
        btn.backgroundColor = UIColor.randomColor()
        
//        btn.setTitle("sdf", forState: .Normal)
        self.view.addSubview(btn)
    }
    
    func configSubViews() -> Void {
        let menuVc = MenuViewController()
        menuContainerView = menuVc.view
        menuContainerView.frame = CGRectMake(0, 0,50, ScreenHeight)
        
        scrollView = UIScrollView()
        scrollView.frame = self.view.bounds
        scrollView.delegate = self
        view.addSubview(scrollView)
        
        scrollView.addSubview(menuContainerView)
        
        detailViewController = DetailViewController()
        detailViewController?.view.frame = CGRectMake(50, 0, ScreenWidth - 50, ScreenHeight)
        scrollView.addSubview((detailViewController?.view)!)
    
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    

}
